# Start w/ Windows Update
Enable-UAC
Enable-MicrosoftUpdate
Install-WindowsUpdate -AcceptEula

# Disable Update/UAC while Configuring
Disable-MicrosoftUpdate
Disable-UAC

# Basic System Configurations
Disable-BingSearch
Disable-GameBarTips

Set-WindowsExplorerOptions -EnableShowHiddenFilesFoldersDrives -EnableShowProtectedOSFiles -EnableShowFileExtensions
Set-TaskbarOptions -Size Small -Dock Top -Combine Full -Lock
Set-TaskbarOptions -Size Small -Dock Top -Combine Full -AlwaysShowIconsOn

# Attempt to Stop Cortana and web searching
New-Item -Path 'HKLM:\SOFTWARE\Microsoft\Windows Search' -Name 'Windows Search' -ItemType Key
New-ItemProperty -Path 'HKLM:\SOFTWARE\Policies\Microsoft\Windows\Windows Search' -Name AllowCortana -Type DWORD -Value 0
New-ItemProperty -Path 'HKLM:\SOFTWARE\Policies\Microsoft\Windows\Windows Search' -Name BingSearchEnabled -Type DWORD -Value 0
New-ItemProperty -Path 'HKLM:\SOFTWARE\Policies\Microsoft\Windows\Windows Search' -Name ConnectedSearchUseWeb -Type DWORD -Value 0
New-ItemProperty -Path 'HKLM:\SOFTWARE\Policies\Microsoft\Windows\Windows Search' -Name DisableWebSearch -Type DWORD -Value 1

# Better File Explorer
Set-ItemProperty -Path HKCU:\Software\Microsoft\Windows\CurrentVersion\Explorer\Advanced -Name NavPaneExpandToCurrentFolder -Value 1		
Set-ItemProperty -Path HKCU:\Software\Microsoft\Windows\CurrentVersion\Explorer\Advanced -Name NavPaneShowAllFolders -Value 1		
Set-ItemProperty -Path HKCU:\Software\Microsoft\Windows\CurrentVersion\Explorer\Advanced -Name MMTaskbarMode -Value 2

# Chocolatey Packages
# Some of these installs are duplicated below — the redundancy is for readability's sake
cinst -y 7zip
cinst -y autohotkey
cinst -y cmder
cinst -y curl
cinst -y docker-for-windows
cinst -y firefox
cinst -y git.install
cinst -y googlechrome
cinst -y putty.install
cinst -y puretext 
cinst -y sysinternals
cinst -y vagrant
cinst -y vscode
cinst -y wsltty

# Enable Windows Features
cinst -y Microsoft-Hyper-V -source WindowsFeatures
cinst -y Microsoft-Windows-Subsystem-Linux -source WindowsFeatures
iwr -uri https://aka.ms/wsl-ubuntu-1804 -OutFile Ubuntu1804.appx -UseBasicParsing
Add-AppxPackage .\Ubuntu1804.appx

# Install/Configure X Server for WSL
choco install cyg-get -y # install cygwin
refreshenv
cyg-get xorg-server xinit # install cygwin/x

# Install fonts
cinst -y hackfont firacode inconsolata dejavufonts robotofonts droidfonts

# Configure VS Code
# code --install-extension EditorConfig.EditorConfig
# code --install-extension vscodevim.vim
# code --install-extension eamodio.gitlens
# code --install-extension gerane.Theme-Paraisodark
# code --install-extension PeterJausovec.vscode-docker
# code --install-extension ms-vscode.PowerShell
# code --install-extension christian-kohler.path-intellisense
# code --install-extension robertohuertasm.vscode-icons
# code --install-extension streetsidesoftware.code-spell-checker

# Install/Configure Git
cinst -y git --params "/GitAndUnixToolsOnPath"
refreshenv

git config --global set core.symlinks true
git config --global set core.autocrlf input
git config --global set core.eol lf
git config --global set color.status auto
git config --global set color.diff auto
git config --global set color.branch auto
git config --global set color.interactive auto
git config --global set color.ui true
git config --global set color.pager true
git config --global set color.showbranch auto

# Configure WSLTTY
choco install -y wsltty

# Finish wsltty setup by setting up shortcuts
$wsl_gen_short = 'C:\ProgramData\Microsoft\Windows\Start Menu\Programs\WSL Generate Shortcuts.lnk'
if(Test-Path $wsl_gen_short) {
    Invoke-Item $wsl_gen_short
}

## Configure wsltty
Copy-Item '.\config_files\wsltty\paraiso_dark.mintty' -Destination "$env:APPDATA\wsltty\themes"
Copy-Item '.\config_files\wsltty\config' -Destination "$env:APPDATA\wsltty\config"

# Setup weasel-pageant
$url = 'https://github.com/vuori/weasel-pageant/releases/download/v1.1/weasel-pageant-1.1.zip'
$archive = 'C:\tools\weasel-pageant-1-1.zip'
if(!(Test-Path $archive)) {
    Write-Host "[installer.weasel-pageant] Downloading..."
    [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
    Invoke-WebRequest -Uri "$url" -OutFile "$archive"

    if(Test-Path "$archive") {
        $zipfile = Get-Item "$archive"
        Write-Host "[installer.weasel-pageant] Downloaded successfully"
        Write-Host "[installer.weasel-pageant] Extracting $archive to ${zipfile.DirectoryName}..."
        Expand-Archive $archive -DestinationPath $zipfile.DirectoryName
    } else {
        Write-Error "[installer.weasel-pageant] Download failed"
    }
}

# Restore Temporary Settings
Enable-UAC
Enable-MicrosoftUpdate
Install-WindowsUpdate -AcceptEula

if (!(Get-Command 'wsl' -ErrorAction SilentlyContinue)) {
  Write-Error @'
You need Windows Subsystem for Linux setup before the rest of this script can run.

See https://docs.microsoft.com/en-us/windows/wsl/install-win10 for more information.
'@
  Exit
}

if ((wsl awk '/^ID=/' /etc/*-release | wsl awk -F'=' '{ print tolower(\$2) }') -ne 'ubuntu') {
  Write-Error 'Ensure Windows Subsystem for Linux is setup to run the Ubuntu distribution'
  Exit
}

if ((wsl awk '/^DISTRIB_RELEASE=/' /etc/*-release | wsl awk -F'=' '{ print tolower(\$2) }') -lt 16.04) {
  Write-Error 'You need to install a minimum of Ubuntu 16.04 Xenial Xerus before running this script'
  Exit
}
$windows_bash_script_path = [regex]::Escape($PSScriptRoot) + '\\install.sh'
$linux_bash_script_path=(wsl wslpath -a "$windows_bash_script_path")
wsl cp "$linux_bash_script_path" "/tmp/"

wsl bash -c "/tmp/install.sh"

refreshenv

# Install Ansible in WSL
wsl bash -c "apt update && apt install python-pip python-dev libffi-dev libssl-dev"
wsl bash -c "pip install ansible --user"
wsl bash -c "echo 'PATH=$HOME/.local/bin:$PATH' >> ~/.bashrc"

refreshenv